package com.zja.www.service.impl;

import com.zja.www.bean.Result;
import com.zja.www.dao.impl.UserDaoImpl;
import com.zja.www.entity.User;
import com.zja.www.service.UserService;
import com.zja.www.bean.Result;
import java.util.List;

/**
 * @author ja
 * @create 2025/3/28
 * @description 用户service业务层接口实现类
 */
public class UserServiceImpl implements UserService {

    private UserDaoImpl userDao = new UserDaoImpl();

    @Override
    public Result login(String userName, String password) {
        User user = userDao.getUserByUsername(userName);
        if (user == null) {
            return Result.fail("用户名不存在");
        }
        boolean loginSuccess = userDao.login(userName, password);
        if (loginSuccess) {
            return Result.success("登录成功", user);  // 返回User对象
        } else {
            return Result.fail("密码错误");
        }
    }

    @Override
    public boolean addUser(User user) {
        if(user == null || user.getUserName() == null) {
            return false;
        }

        if(userDao.getUserByUsername(user.getUserName()) != null){
            return false;
        }
        return userDao.insert(user);

    }

    @Override
    public User getUserById(int id) {
        if(userDao.getUserById(id) != null){
            return userDao.getUserById(id);
        }
        return null;
    }

    @Override
    public List<User> getAllUsers() {
        if(userDao.getAllUser() != null){
            return userDao.getAllUser();
        }
      return null;
    }

    @Override
    public boolean updateUser(User user) {
        if(user == null || userDao.getUserById(user.getId()) == null) {
            return false;
        }
        return userDao.updateUser(user);
    }

    @Override
    public boolean deleteUser(int id) {
        if(userDao.getUserById(id) == null) {
            return false;
        }
        return userDao.deleteUser(id);
    }

    @Override
    public User getUserByUsername(String username) {
        if(userDao.getUserByUsername(username) != null){
            return userDao.getUserByUsername(username);
        }

        return null;
    }
}
